package com.rpoonolly.simplegame.editor;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.rpoonolly.simplegame.Config;
import com.rpoonolly.simplegame.textures.TextureFileDescriptor;
import com.rpoonolly.simplegame.textures.TextureId;
import com.rpoonolly.simplegame.textures.TextureLoader;

public class EditorPanel extends JDialog implements ActionListener, ItemListener {
	private static final long serialVersionUID = 1L;
	
	private static final String[] entityTypes = { Config.ENT_GRP_ENEMIES, Config.ENT_GRP_GHOSTS };
	private static final TextureFileDescriptor[] worldGridTextureMaps = Config.WORLD_GRID_TEXTURE_FILES;
	
	private static final int TEXTURE_MAP_GRID_HGAP = 1;
	private static final int TEXTURE_MAP_GRID_VGAP = 1;
	
	private TextureLoader textureLoader;
	
	private JPanel editorPanel;
	private LayoutManager editorPanelLayout;
	
	private JComboBox<String> entityTypeSelector;
	private JComboBox<TextureFileDescriptor> textureMapSelector;
	
	private TextureId selectedTexture;
	private JLabel selectedTextureDisplay;
	
	private JPanel textureSelectionPanel;
	private GridLayout textureSelectionLayout;
	
	public EditorPanel(TextureLoader textureLoader) {
		this.textureLoader = textureLoader;
		
		setSize(500, 500);
		setLocation(500, 0);
		
		GridBagConstraints gc = new GridBagConstraints();
		editorPanelLayout = new GridBagLayout();
		
		editorPanel = new JPanel();
		editorPanel.setLayout(editorPanelLayout);
		
		gc.gridx = 0;
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.gridwidth = 4;
		entityTypeSelector = new JComboBox<String>(entityTypes);
		editorPanel.add(entityTypeSelector, gc);
		
		gc.gridy = 3;
		textureMapSelector = new JComboBox<TextureFileDescriptor>(worldGridTextureMaps);
		textureMapSelector.addItemListener(this);
		editorPanel.add(textureMapSelector, gc);
		
		gc.gridy = 4;
		gc.gridx = 1;
		gc.gridwidth = 1;
		editorPanel.add(new JLabel("Selected:"), gc);
		
		gc.gridx = 2;
		selectedTexture = new TextureId(getSelectedTextureMap(), 0, 0);
		selectedTextureDisplay = new JLabel();
		selectedTextureDisplay.setIcon(new ImageIcon(textureLoader.loadTexture(selectedTexture)));
		editorPanel.add(selectedTextureDisplay, gc);

		gc.gridy = 5;
		textureSelectionLayout = new GridLayout();
		textureSelectionLayout.setHgap(TEXTURE_MAP_GRID_HGAP);
		textureSelectionLayout.setVgap(TEXTURE_MAP_GRID_VGAP);
		textureSelectionPanel = new JPanel(textureSelectionLayout);
		editorPanel.add(textureSelectionPanel, gc);
		
		setContentPane(editorPanel);
	}
	
	public String getSelectedEntityType() {
		return (String) entityTypeSelector.getSelectedItem();
	}
	
	public TextureFileDescriptor getSelectedTextureMap() {
		return (TextureFileDescriptor) textureMapSelector.getSelectedItem();
	}
	
	public synchronized TextureId getSelectedTexture() {
		return selectedTexture;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		TextureSelectionButton selectedButton = (TextureSelectionButton) e.getSource();
		
		synchronized(this) {
			selectedTexture = selectedButton.getTexture();
			selectedTextureDisplay.setIcon(new ImageIcon(textureLoader.loadTexture(selectedTexture)));
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		reloadTextureSelectionPanel(getSelectedTextureMap());
	}
	
	private void reloadTextureSelectionPanel(TextureFileDescriptor textureMap) {
		textureSelectionPanel.removeAll();
		
		textureSelectionLayout.setRows(textureMap.mapWidth);
		textureSelectionLayout.setColumns(textureMap.mapHeight);
		
		Icon icon;
		TextureId texture;
		TextureSelectionButton button;
		for (int row = 0; row < textureMap.mapWidth; row++) {
			for (int col = 0; col < textureMap.mapHeight; col++) {
				texture = new TextureId(textureMap, row, col);
				icon = new ImageIcon(textureLoader.loadTexture(texture));
				button = new TextureSelectionButton(texture, icon);
				button.addActionListener(this);
			
				textureSelectionPanel.add(button);
			}
		}
		
		validate();
		repaint();
	}

	private final class TextureSelectionButton extends JButton {
		private static final long serialVersionUID = 1L;
		private final TextureId texture;
		
		public TextureSelectionButton(TextureId texture, Icon icon) {
			super(icon);
			this.texture = texture;
		}

		public TextureId getTexture() {
			return texture;
		}
	}
}
